package com.pinyougou.manager.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.pinyougou.common.PageResult;
import com.pinyougou.pojo.Brand;
import com.pinyougou.service.BrandService;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/brand")
public class BrandController
{
    /*
        调用服务接口代理对象
        timeout:调用服务接口超时毫秒数
     */
    //@Autowired
    @Reference(timeout = 10000)
    private BrandService brandService;

    /**
     * 查询所有品牌
     */
    @GetMapping("/findAll")
    public List<Brand> findAll()
    {
        return brandService.findAll();
    }

    /*分页查询*/
    @GetMapping("/findByPage")
    public PageResult findByPage(Brand brand, Integer page, @RequestParam(defaultValue = "10") Integer rows)
    {
        return brandService.findByPage(brand, page, rows);
    }

    //保存
    @PostMapping("/save")
    public boolean save(@RequestBody Brand brand)
    {
        try
        {
            brandService.save(brand);
            return true;
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        //return "redirect:/brand/findAll";
        return false;
    }

    //修改
    @PostMapping("/update")
    public boolean update(@RequestBody Brand brand)
    {
        try
        {
            brandService.update(brand);
            return true;
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        //return "redirect:/brand/findAll";
        return false;
    }

    /**
     * 删除所选的记录
     */
    @GetMapping("/delete")
    public boolean delete(Long[] ids)
    {
        try
        {
            brandService.deleteAll(ids);
            return true;
        }
        catch (Exception e)
        {
            e.printStackTrace();
            return false;
        }
    }
    /** 查找所有的品牌名称和id */
    @GetMapping("/findBrandList")
    public List<Map<String,Object>> findBrandList()
    {
        return brandService.findIdNameList();
    }
}